Ext.define('archives.controller.MainFrame', {
			extend : 'Ext.app.Controller',

			refs : [{
						ref : 'header',
						selector : 'viewport > #mainHeader'
					}, {
						ref : 'menu',
						selector : 'viewport > #mainMenu'
					}, {
						ref : 'center',
						selector : 'viewport > #mainCenter'
					}, {
						ref : 'footer',
						selector : 'viewport > #mainFooter'
					}],

			init : function() {
				this.control({
							'viewport > #mainHeader' : {
								beforerender : this.onMainHeaderBeforeRender
							},
							'viewport > #mainMenu' : {
								beforerender : this.onMainMenuBeforeRender
							},
							'viewport > #mainCenter' : {
								beforerender : this.onMainCenterBeforeRender
							},
							'viewport > #mainFooter' : {
								beforerender : this.onMainFooterBeforeRender
							}
						})
			},

			onMainHeaderBeforeRender : function(item) {
				var btnSignOut = this.getHeader().down('#btnSignOut');
				btnSignOut.on('click', this.onSignOut, this);
				
				var btnSwtichLanguage = this.getHeader().down('#btnSwtichLanguage');
				btnSwtichLanguage.on('click', this.onSwtichLanguage, this);
				
			},
			onMainMenuBeforeRender : function(item) {
				this.getMenu().items.get(0).items.get(0).on('itemclick',
						this.onTreeNodeClick, this);
			},
			onMainCenterBeforeRender : function(item) {
			},
			onMainFooterBeforeRender : function(item) {
			},
			onTreeNodeClick : function(view, rec) {
				if (!rec.get('leaf'))
					return;

				var center = this.getCenter();
				var tabId = 'tab-' + rec.get('id');
				var tabActive = center.items.get(tabId);
				if (!tabActive) {
					
					var pageUrl = rec.get('pageUrl');
					var controllerName = (pageUrl === null || pageUrl.length === 0) ? AlUtil.firstUpperCase(rec.get('menuCode')) : pageUrl ;
					
					var controller = this.getController(controllerName);
					
					/*var mainViewName = controller.mainView;
					if (mainViewName == null || Ext.isEmpty(mainViewName))
					{
						mainViewName = AlUtil.firstLowerCase(rec.get('menuCode')) + ".Main";
					}*/
					var mainView = controller.getMainView();//controller.getView(mainViewName).create();
					
					tabActive = center.add({
								itemId : tabId,
								title : rec.get('text'),
								layout : 'fit',
								closable : true,
								border : false,
								items : [mainView]
							});
					tabActive.on('close', 
									function(panel, eOpts) {
										mainView.close();
									}, this);
									
					controller.refreshViewData();
				}
				tabActive.show();
			},
			onSignOut : function(_this, e, eOpts) {
				Ext.Ajax.request({
							url : G_SMPACKAGE + 'logoff.action',
							success : function(response) {
								location.href = Ext.get("basePath").dom.value;
							}
						});
			},
			onSwtichLanguage : function(_this, e, eOpts) {
				Ext.Ajax.request({
					url : G_SMPACKAGE + 'swtichLanguage.action',
					success : function(response, options) {
						location.href = Ext.get("basePath").dom.value+G_SMPACKAGE+'welcome.action';
					}
				});
			}
		})